﻿// ....................................................................................................................
//	Author: Copyright (C) 2012 Ing. Marek Hubal
//	Email: hubal@acasoftware.org
// ....................................................................................................................

using System;

namespace core
{
    /// <summary>Trieda IAppForm</summary>
    public class IAppForm : IForm
    {
        protected IToolStrip FMainMenu; // Hlavne menu aplikacie
        protected System.Windows.Forms.NotifyIcon FTrayIcon; // Tray ikona
        protected ITabControl FTabCtrl; // Tabcontrol pre aplikacne zalozky
        protected bool FCanPanelActivate; // Priznak moznosti aktivacie panela
        protected int FStartActivePanel; // Pozicia aktivneho panela pre spustenie

        /// <summary>Property: ActivePanel</summary>
        public IBaseAppPanel ActivePanel
        {
            get
            {
                ITabPage P = (ITabPage)FTabCtrl.SelectedTab;
                return P == null ? null : P.AppPanel;
            }
        }
        /// <summary>Property: ActivePanelCMDGroupID</summary>
        public int ActivePanelCMDGroupID
        {
            get
            {
                IBaseAppPanel P = ActivePanel;
                return P == null ? ICmd.CMD_GROUP_COMMON : P.CMDGroupID;
            }
        }
        /// <summary>Property: CanPanelActivate</summary>
        public bool CanPanelActivate
        {
            get
            {
                return FCanPanelActivate;
            }
            set
            {
                FCanPanelActivate = value;
            }
        }
        /// <summary>Property: CanCloseAllPanels</summary>
        public bool CanCloseAllPanels
        {
            get
            {
                foreach (ITabPage TP in FTabCtrl.TabPages) // Prejdem vsetky panely
                {
                    if (!TP.AppPanel.CanClose) return false; // Neda sa zatvorit
                }
                return true; // Vsetky sa daju zatvorit
            }
        }

        /// <summary>Konstruktor</summary>
        public IAppForm()
        {
            IApp.AppForm = this; // Ulozim instanciu
            FCanPanelActivate = true;
        }

        /// <summary>Metoda: OnCreateForm</summary>
        /// <returns>true / false</returns>
        protected override bool OnCreateForm()
        {
            if (!base.OnCreateForm()) return false; // Volam predchodcu

            // Nastavenie properties
            MinimizeBox = true; MaximizeBox = true; StartPosition = System.Windows.Forms.FormStartPosition.Manual; ShowIcon = true; ShowInTaskbar = true;
            FTrayIcon = null;

            if (IApp.Settings.AppForm_RememberLastPosition)
            {
                if (IApp.Settings.AppForm_Left < 0) IApp.Settings.AppForm_Left = 100;
                if (IApp.Settings.AppForm_Top < 0) IApp.Settings.AppForm_Top = 100;
                SetBounds(IApp.Settings.AppForm_Left, IApp.Settings.AppForm_Top, IApp.Settings.AppForm_Width, IApp.Settings.AppForm_Height); // Nastavim poslednu poziciu a rozmery
                WindowState = IApp.Settings.AppForm_LastMaximized || IApp.Settings.AppForm_ForceMaximize ? System.Windows.Forms.FormWindowState.Maximized : System.Windows.Forms.FormWindowState.Normal;
            }
            else
            {
                SetBounds(100, 100, 600, 500); // Nastavim default poziciu a rozmery
                WindowState = System.Windows.Forms.FormWindowState.Normal;
            }

            FMainMenu = new IToolStrip(); FMainMenu.Dock = System.Windows.Forms.DockStyle.Top; // Vytvorim hlavne menu
            FTabCtrl = new ITabControl(); FTabCtrl.Dock = System.Windows.Forms.DockStyle.Fill; // Vytvorim tabcontrol
            FTabCtrl.TabAlignment = IApp.Settings.AppForm_TabPosition; FTabCtrl.TabStop = false;
            FTabCtrl.ContextMenuStrip = new System.Windows.Forms.ContextMenuStrip(); FTabCtrl.ContextMenuStrip.Items.Add(""); // Popup menu
            

            FTabCtrl.ImageList = new System.Windows.Forms.ImageList(); // Vytvorim image list pre ikony zaloziek
            FTabCtrl.ImageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;
            //FTabCtrl.ImageList.ImageSize = new System.Drawing.Size(16, 16);
            FTabCtrl.ImageList.Images.Add(core.Properties.Resources.tab_work);
            FTabCtrl.ImageList.Images.Add(core.Properties.Resources.tab_info);
            FTabCtrl.ImageList.Images.Add(core.Properties.Resources.tab_question);
            FTabCtrl.ImageList.Images.Add(core.Properties.Resources.tab_error);

            int C = IApp.GlobalData.AppPanels_StartSettings.GetParam("COUNT", 0);
            if (C == 0)
            {
                AddNewPanel(IBaseAppPanel.APP_PANEL_ID_FILE_EXPLORER);
                FStartActivePanel = 0;
            }
            else
            {
                for (int i = 0; i < C; i++) // Vytvorim panely
                {
                    AddNewPanel(IApp.GlobalData.AppPanels_StartSettings.GetParam(string.Format("ID_{0}", i), IBaseAppPanel.APP_PANEL_ID_FILE_EXPLORER), ICore.Base64Decode(IApp.GlobalData.AppPanels_StartSettings.GetParam(string.Format("INIT_STR_{0}", i), "")));
                }
                FStartActivePanel = IApp.GlobalData.AppPanels_StartSettings.GetParam("ACTIVE_PANEL", 0);
            }

            Controls.AddRange(new System.Windows.Forms.Control[] { FTabCtrl, FMainMenu }); // Zaradim do controls

            FTabCtrl.SelectedIndexChanged += Event_SelectedIndexChanged;
            FTabCtrl.ContextMenuStrip.Opening += Event_ContentMenuOpening;

            Text = IApp.App.AppTitle; // Nastavim nazov
            return true; // Vsetko OK
        }
        /// <summary>Uzatvorenie formulara</summary>
        protected void CloseForm()
        {
            if (IApp.Settings.AppForm_RememberLastPosition) // Ulozit posledny stav?
            {
                if (!IApp.Settings.AppForm_ForceMaximize) // Nie je defaultna maximalizacia?
                {
                    IApp.Settings.AppForm_LastMaximized = FLastMaximized; // Ulozim stav poslednej maximalizacie
                    if (!FLastMaximized) // Ak nie je okno maximalizovane...
                    {
                        IApp.Settings.AppForm_Left = Left;
                        IApp.Settings.AppForm_Top = Top;
                        IApp.Settings.AppForm_Width = Width;
                        IApp.Settings.AppForm_Height = Height;
                    }
                }
            }
            FTrayIcon = (System.Windows.Forms.NotifyIcon)ICore.DisposeObject(FTrayIcon);

            CloseAllPanels(); // Uzatvorim panely
        }
        /// <summary>Uzatvorenie vsetkych panelov</summary>
        protected void CloseAllPanels()
        {
            FTabCtrl.SelectedIndexChanged -= Event_SelectedIndexChanged; // Odstranim event

            IApp.GlobalData.AppPanels_StartSettings.SetParam("COUNT", FTabCtrl.TabCount);
            IApp.GlobalData.AppPanels_StartSettings.SetParam("ACTIVE_PANEL", ActivePanel.TabPosition);

            CanPanelActivate = false;
            FTabCtrl.SuspendLayout();
            foreach (ITabPage Page in FTabCtrl.TabPages) // Rusim vsetky zalozky
            {
                Page.AppPanel.SaveSettings(); // Ulozim nastavenie
                IApp.GlobalData.AppPanels_StartSettings.SetParam(string.Format("ID_{0}", Page.AppPanel.TabPosition), Page.AppPanel.ID);
                IApp.GlobalData.AppPanels_StartSettings.SetParam(string.Format("INIT_STR_{0}", Page.AppPanel.TabPosition), ICore.Base64Encode(Page.AppPanel.InitString));
                Page.AppPanel.Close(); // Zrusim panel
                Page.AppPanel.Dispose();
            }
            FTabCtrl.TabPages.Clear(); // Zrusim vsetky zalozky
        }
        /// <summary>Najdenie pozicie panela</summary>
        /// <param name="pPanel">panel</param>
        /// <returns>pozicia alebo -1</returns>
        protected int FindPanelIndex(IBaseAppPanel pPanel)
        {
            int C = FTabCtrl.TabCount; // Pocet zaloziek
            for (int i = 0; i < C; i++) // Prechadzam
            {
                if (((ITabPage)FTabCtrl.TabPages[i]).AppPanel == pPanel) return i;
            }
            return -1; // Nic sa nenaslo
        }
        /// <summary>Vytvorenie menu zaloziek</summary>
        /// <param name="pParent">rodicovska kolekcia</param>
        protected void CreateTabsMenu(System.Windows.Forms.ToolStripItemCollection pParent)
        {
            int ID = ActivePanel == null ? IBaseAppPanel.APP_PANEL_ID_NONE : ActivePanel.ID;
            switch (ID)
            {
                case IBaseAppPanel.APP_PANEL_ID_FILE_EXPLORER:
                    {
                        IToolStrip.CreateCMDMenuItem(pParent, lang.MENU_ITEM_TEXT_FILE_EXPLORER_TABS_NEW, ICmd.CMD_ID_FILE_EXPLORER_NEW_TAB);
                        IToolStrip.CreateSeparator(pParent);
                        IToolStrip.CreateCMDMenuItem(pParent, lang.MENU_ITEM_TEXT_FILE_EXPLORER_TABS_NEW_CURRENT_PATH, ICmd.CMD_ID_FILE_EXPLORER_NEW_TAB_CURRENT_PATH, ICmd.CMD_APP_TARGET_ACTIVE);
                        IToolStrip.CreateCMDMenuItem(pParent, lang.MENU_ITEM_TEXT_FILE_EXPLORER_TABS_NEW_CURRENT_ITEM, ICmd.CMD_ID_FILE_EXPLORER_NEW_TAB_CURRENT_ITEM, ICmd.CMD_APP_TARGET_ACTIVE);
                        IToolStrip.CreateSeparator(pParent);
                        IToolStrip.CreateCMDMenuItem(pParent, lang.MENU_ITEM_TEXT_TABS_DUPLICATE, ICmd.CMD_ID_TAB_DUPLICATE);
                        IToolStrip.CreateSeparator(pParent);
                        IToolStrip.CreateCMDMenuItem(pParent, lang.MENU_ITEM_TEXT_TABS_RENAME, ICmd.CMD_ID_TAB_RENAME);
                        IToolStrip.CreateSeparator(pParent);
                        IToolStrip.CreateCMDMenuItem(pParent, lang.MENU_ITEM_TEXT_TABS_CLOSE, ICmd.CMD_ID_TAB_CLOSE_ACTIVE);
                        IToolStrip.CreateCMDMenuItem(pParent, lang.MENU_ITEM_TEXT_TABS_CLOSE_ALL, ICmd.CMD_ID_TAB_CLOSE_ALL);
                        IToolStrip.CreateCMDMenuItem(pParent, lang.MENU_ITEM_TEXT_TABS_CLOSE_ALL_RIGHT, ICmd.CMD_ID_TAB_CLOSE_ALL_RIGHT);
                        IToolStrip.CreateSeparator(pParent);
                        IToolStrip.CreateCMDMenuItem(pParent, lang.MENU_ITEM_TEXT_TABS_PREV, ICmd.CMD_ID_TAB_PREVIOUS);
                        IToolStrip.CreateCMDMenuItem(pParent, lang.MENU_ITEM_TEXT_TABS_NEXT, ICmd.CMD_ID_TAB_NEXT);
                    } break;
                default:
                    {
                        IToolStrip.CreateCMDMenuItem(pParent, lang.MENU_ITEM_TEXT_FILE_EXPLORER_TABS_NEW, ICmd.CMD_ID_FILE_EXPLORER_NEW_TAB);
                        IToolStrip.CreateSeparator(pParent);
                        IToolStrip.CreateCMDMenuItem(pParent, lang.MENU_ITEM_TEXT_TABS_DUPLICATE, ICmd.CMD_ID_TAB_DUPLICATE);
                        IToolStrip.CreateSeparator(pParent);
                        IToolStrip.CreateCMDMenuItem(pParent, lang.MENU_ITEM_TEXT_TABS_RENAME, ICmd.CMD_ID_TAB_RENAME);
                        IToolStrip.CreateSeparator(pParent);
                        IToolStrip.CreateCMDMenuItem(pParent, lang.MENU_ITEM_TEXT_TABS_CLOSE, ICmd.CMD_ID_TAB_CLOSE_ACTIVE);
                        IToolStrip.CreateCMDMenuItem(pParent, lang.MENU_ITEM_TEXT_TABS_CLOSE_ALL, ICmd.CMD_ID_TAB_CLOSE_ALL);
                        IToolStrip.CreateCMDMenuItem(pParent, lang.MENU_ITEM_TEXT_TABS_CLOSE_ALL_RIGHT, ICmd.CMD_ID_TAB_CLOSE_ALL_RIGHT);
                        IToolStrip.CreateSeparator(pParent);
                        IToolStrip.CreateCMDMenuItem(pParent, lang.MENU_ITEM_TEXT_TABS_PREV, ICmd.CMD_ID_TAB_PREVIOUS);
                        IToolStrip.CreateCMDMenuItem(pParent, lang.MENU_ITEM_TEXT_TABS_NEXT, ICmd.CMD_ID_TAB_NEXT);
                    } break;
            }
        }

        /// <summary>Metoda: OnFormClosed</summary>
        /// <param name="e">parameter</param>
        protected override void OnFormClosed(System.Windows.Forms.FormClosedEventArgs e)
        {
            IApp.App.Exit(0); // Ukoncim aplikaciu
        }
        /// <summary>Metoda: OnFormClosing</summary>
        /// <param name="e">parameter</param>
        protected override void OnFormClosing(System.Windows.Forms.FormClosingEventArgs e)
        {
            if (!CanCloseAllPanels) // Skontrolujem ci moze zavriet aplikaciu
            {
                IApp.ShowInfo(lang.APPFORM_TEXT_7);
                e.Cancel = true;
                return;
            }

            foreach (IForm F in System.Windows.Forms.Application.OpenForms) // Skontrolujem okna
            {
                if (!F.CanCloseForm)
                {
                    IApp.ShowInfo(lang.APPFORM_TEXT_7_1);
                    e.Cancel = true;
                    return;
                }
            }

            if (IApp.Settings.App_ShowQuitQuestion) // Potvrdit ukoncenie?
            {
                if (!IApp.ShowQuestion(lang.APPFORM_TEXT_1)) // Opytam sa...
                {
                    e.Cancel = true;
                    return;
                }
            }
            CloseForm(); // Volam uzatvorenie okna
        }
        /// <summary>Metoda: OnSizeChanged</summary>
        /// <param name="e">parameter</param>
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e); // Volam predchodcu
            if (WindowState == System.Windows.Forms.FormWindowState.Minimized) // Bolo okna minimalizovane?
            {
                if (IApp.Settings.AppForm_MinimizeToTry) // Minimalizovat do tray ikony?
                {
                    FTrayIcon = new System.Windows.Forms.NotifyIcon(); // Vytvorenie ikony
                    FTrayIcon.Icon = core.Properties.Resources.main_icon; // Nastavim ikonu
                    FTrayIcon.Text = lang.APPFORM_TEXT_5;
                    FTrayIcon.DoubleClick += Event_DblClick_TrayIcon;
                    FTrayIcon.Visible = true; // Zobrazim
                    Hide(); // Skryjem hlavne okno
                }
            }
        }
        /// <summary>Event: DoubleClick pre FTrayIcon</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_DblClick_TrayIcon(object pSender, EventArgs e)
        {
            FTrayIcon = (System.Windows.Forms.NotifyIcon)ICore.DisposeObject(FTrayIcon); // Uvolnim tray ikonu
            if (FLastMaximized)
            {
                WindowState = System.Windows.Forms.FormWindowState.Maximized;
                base.Show();
            }
            else
            {
                base.Show();
                WindowState = System.Windows.Forms.FormWindowState.Normal;
            }
            Activate();
        }
        /// <summary>Event: SelectedIndexChanged</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_SelectedIndexChanged(object pSender, EventArgs e)
        {
            if (FTabCtrl.Tag == null) 
                SetActivePanel(FTabCtrl.SelectedIndex); // Volam aktivaciu
        }
        /// <summary>Event: Opening</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_ContentMenuOpening(object pSender, System.ComponentModel.CancelEventArgs e)
        {
            System.Drawing.Point P = MousePosition;
            bool IsIn = false;
            for (int i = 0; i < FTabCtrl.TabCount; i++) // Testujem ci kurzor je nad niektorou zo zaloziek
            {
                System.Drawing.Rectangle R = FTabCtrl.RectangleToScreen(FTabCtrl.GetTabRect(i));
                if (R.Contains(P))
                {
                    IsIn = true;
                    break;
                }
            }
            if (!IsIn) // Nie je?
            {
                e.Cancel = true; // Zrusim menu
                return;
            }

            System.Windows.Forms.ContextMenuStrip M = (System.Windows.Forms.ContextMenuStrip)pSender;
            M.Items.Clear(); // Zrusim aktualny obsah
            CreateTabsMenu(M.Items); // Vytvorim menu
        }
        /// <summary>Metoda: Shown</summary>
        /// <param name="e">parameter</param>
        protected override void OnShown(EventArgs e)
        {
            SetActivePanel(FStartActivePanel); // Aktivujem panel
        }

        /// <summary>Aktualizacia hlavneho menu</summary>
        public void UpdateMainMenu()
        {
            FMainMenu.Items.Clear(); // Zrusim aktualny obsah

            if (ActivePanel != null) ActivePanel.CreatePanelMenu(FMainMenu.Items); // Volam vytvorenie menu aktivneho panela

            IToolStripDropDownButton B;

            B = FMainMenu.CreateDropDownButton(lang.MENU_ITEM_TEXT_HELP); B.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
            B.CreateCMDMenuItem(lang.MENU_ITEM_TEXT_HELP_HELP);
            B.CreateSeparator();
            B.CreateCMDMenuItem(lang.MENU_ITEM_TEXT_HELP_EDIT_SETTINGS, ICmd.CMD_ID_EDIT_SETTINGS);
            B.CreateCMDMenuItem(lang.MENU_ITEM_TEXT_HELP_EDIT_VISUAL_THEME, ICmd.CMD_ID_EDIT_VISUAL_THEME);
            B.CreateCMDMenuItem(lang.MENU_ITEM_TEXT_HELP_EDIT_SHORTCUTS, ICmd.CMD_ID_EDIT_SHORTCUTS);
            B.CreateSeparator();
            B.CreateCMDMenuItem(lang.MENU_ITEM_TEXT_HELP_VISIT_HOMEPAGE, ICmd.CMD_ID_VISIT_HOMEPAGE);
            B.CreateCMDMenuItem(lang.MENU_ITEM_TEXT_CHECK_UPDATE, ICmd.CMD_ID_CHECK_UPDATE);
            B.CreateSeparator();
            B.CreateCMDMenuItem(lang.MENU_ITEM_TEXT_HELP_SHOW_ABOUT, ICmd.CMD_ID_SHOW_ABOUT);

            FMainMenu.CreateSeparator(true);
            B = FMainMenu.CreateDropDownButton(lang.MENU_ITEM_TEXT_TABS); B.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
            CreateTabsMenu(B.DropDownItems);
        }
        /// <summary>Aktualizacia objektu po zmene nastavenia</summary>
        public void UpdateAfterChangeSettings()
        {
            FTabCtrl.TabAlignment = IApp.Settings.AppForm_TabPosition;

            int C = FTabCtrl.TabCount;
            for (int i = 0; i < C; i++) // Vsetky zalozky...
            {
                ((ITabPage)FTabCtrl.TabPages[i]).AppPanel.UpdateAfterChangeSettings();
            }
        }
        /// <summary>Vykonanie prikazu</summary>
        /// <param name="pCMD_ID">ID prikazu</param>
        /// <param name="pAPP_TARGET">aplikacia</param>
        /// <param name="pPANEL_TARGET">panel</param>
        /// <returns>true / false</returns>
        public bool ExecCMD(int pCMD_ID, int pAPP_TARGET, int pPANEL_TARGET)
        {
            IBaseAppPanel AP = ActivePanel;

            switch (pCMD_ID)
            {
                case ICmd.CMD_ID_TAB_RENAME:
                    {
                        string N = ActivePanel.CustomTabName;
                        if (!IApp.ShowEditor_String(IApp.App.AppTitle, lang.APPFORM_TEXT_6_5, ref N, 32, true)) return true; // Ziskam nazov od uzivatela
                        ActivePanel.TabName = N.Trim();

                        return true; // Prikaz bol vykonany
                    }
                case ICmd.CMD_ID_FILE_EXPLORER_NEW_TAB:
                    {
                        IBaseAppPanel P = AddNewPanel(IBaseAppPanel.APP_PANEL_ID_FILE_EXPLORER); // Vytvorim panel
                        SetActivePanel(P); // Aktivujem
                        return true; // Prikaz bol vykonany
                    }
                case ICmd.CMD_ID_TAB_DUPLICATE:
                    {
                        ActivePanel.DuplicatePanel(); // Vytvorim kopiu
                        return true; // Prikaz bol vykonany
                    }
                case ICmd.CMD_ID_TAB_CLOSE_ACTIVE:
                    {
                        if (FTabCtrl.TabCount < 2) return true; // Posledna zalozka?

                        IBaseAppPanel P = ActivePanel; // Ziskam panel
                        if (!P.CanClose) // Nie je mozne uzatvorit?
                        {
                            IApp.ShowInfo(lang.APPFORM_TEXT_6_1);
                            return true; // Prikaz vykonany
                        }

                        if (!IApp.ShowQuestion(lang.APPFORM_TEXT_6)) return true; // Potvrdim uzatvorenie uzivatelom
                        DeletePanel(P.TabPosition); // Uzatvorim panel

                        return true; // Prikaz bol vykonany
                    }
                case ICmd.CMD_ID_TAB_CLOSE_ALL:
                    {
                        if (FTabCtrl.TabCount < 2) return true; // Posledna zalozka?

                        bool CanCloseAll = true;
                        int ActI = FTabCtrl.SelectedIndex;
                        int C = FTabCtrl.TabCount;

                        for (int i = 0; i < C; i++) // Zistim, ci vsetky panely sa daju zatvorit
                        {
                            if (i == ActI) continue;
                            CanCloseAll &= ((ITabPage)FTabCtrl.TabPages[i]).AppPanel.CanClose;
                        }

                        if (!IApp.ShowQuestion(CanCloseAll ? lang.APPFORM_TEXT_6_2 : lang.APPFORM_TEXT_6_3)) return true; // Potvrdim uzatvorenie
                        DeleteAllPanels(ActI); // Volam zrusenie

                        return true; // Prikaz
                    }
                case ICmd.CMD_ID_TAB_CLOSE_ALL_RIGHT:
                    {
                        int ActI = FTabCtrl.SelectedIndex;
                        if (ActI == FTabCtrl.TabCount - 1) return true; // Posledna zalozka?

                        bool CanCloseAll = true;
                        int C = FTabCtrl.TabCount;

                        for (int i = ActI + 1; i < C; i++) // Zistim, ci vsetky panely sa daju zatvorit
                        {
                            CanCloseAll &= ((ITabPage)FTabCtrl.TabPages[i]).AppPanel.CanClose;
                        }

                        if (!IApp.ShowQuestion(CanCloseAll ? lang.APPFORM_TEXT_6_4 : lang.APPFORM_TEXT_6_3)) return true; // Potvrdim uzatvorenie
                        DeleteAllPanelsOnRight(ActI); // Volam zrusenie

                        return true; // Prikaz
                    }
                case ICmd.CMD_ID_TAB_NEXT:
                    {
                        if (FTabCtrl.TabCount < 2) return true; // Nie je kde sa posuvat?

                        int I = FTabCtrl.SelectedIndex;
                        I = I == FTabCtrl.TabCount - 1 ? 0 : I + 1; // Urcim dalsiu zalozku

                        SetActivePanel(I); // Nastavim novu zalozku
                        return true; // Prikaz
                    }
                case ICmd.CMD_ID_TAB_PREVIOUS:
                    {
                        if (FTabCtrl.TabCount < 2) return true; // Nie je kde sa posuvat?

                        int I = FTabCtrl.SelectedIndex;
                        I = I == 0 ? FTabCtrl.TabCount - 1 : I - 1; // Urcim dalsiu zalozku

                        SetActivePanel(I); // Nastavim novu zalozku
                        return true; // Prikaz
                    }
            }

            switch (pAPP_TARGET) // Podla aplikacie
            {
                case ICmd.CMD_APP_TARGET_NONE: break;
                case ICmd.CMD_APP_TARGET_ACTIVE:
                    {
                        return AP == null ? false : AP.ExecCMD(pCMD_ID, pAPP_TARGET, pPANEL_TARGET); // Volam exec pre aktivny panel
                    }
                case ICmd.CMD_APP_TARGET_ALL:
                    {
                        bool B = false;
                        foreach (ITabPage TP in FTabCtrl.TabPages) // Prejdem vsetky panely
                        {
                            B |= TP.AppPanel.ExecCMD(pCMD_ID, pAPP_TARGET, pPANEL_TARGET); // Volam exec
                        }
                        return B;
                    }
                case ICmd.CMD_APP_TARGET_ALL_INACTIVE:
                    {
                        bool B = false;
                        foreach (ITabPage TP in FTabCtrl.TabPages) // Prejdem vsetky panely
                        {
                            if (TP.AppPanel != AP) B |= TP.AppPanel.ExecCMD(pCMD_ID, pAPP_TARGET, pPANEL_TARGET); // Volam exec
                        }
                        return B;
                    }
                case ICmd.CMD_APP_TARGET_SAME_AS_ACTIVE:
                    {
                        bool B = false;
                        foreach (ITabPage TP in FTabCtrl.TabPages) // Prejdem vsetky panely
                        {
                            if ((TP != null) && (TP.AppPanel.ID == AP.ID)) B |= TP.AppPanel.ExecCMD(pCMD_ID, pAPP_TARGET, pPANEL_TARGET); // Volam exec
                        }
                        return B;
                    }

            }
            return false; // Prikaz nebol vykonany
        }
        /// <summary>Aktualizacia objektu po zmene vizualnej temy</summary>
        public void UpdateAfterChangeVisualTheme()
        {
            int C = FTabCtrl.TabCount;
            for (int i = 0; i < C; i++) // Vsetky zalozky...
            {
                ((ITabPage)FTabCtrl.TabPages[i]).AppPanel.UpdateAfterChangeVisualTheme();
            }
        }
        /// <summary>Zrusenie panela</summary>
        /// <param name="pIndex">pozicia</param>
        public void DeletePanel(int pIndex)
        {
            CanPanelActivate = false;
            FTabCtrl.Tag = 1;
            FTabCtrl.SuspendLayout();

            ITabPage TP = (ITabPage)FTabCtrl.TabPages[pIndex]; // Ziskam zalozku
            if (!TP.AppPanel.CanClose) return; // Neda sa zrusit

            TP.AppPanel.Close(); // Zrusim panel
            TP.AppPanel.Dispose(); // Zrusim panel
            FTabCtrl.TabPages.RemoveAt(pIndex); TP.Dispose(); // Zrusim zalozku

            IBaseAppPanel.DeleteSettings(pIndex); // Zrusim nastavenie
            
            int C = FTabCtrl.TabCount;
            for (int i = pIndex; i < C; i++) // Pomenim pozicie dalsich panelov
            {
                TP = (ITabPage)FTabCtrl.TabPages[i]; // Zalozka
                IBaseAppPanel.MoveSettings(TP.AppPanel.TabPosition, i); // Presuniem parametre
                TP.AppPanel.TabPosition = i; // Nova pozicia
            }

            FTabCtrl.Tag = null;
            CanPanelActivate = true;

            SetActivePanel(pIndex < C - 1 ? pIndex : C - 1); // Aktivujem panel
            FTabCtrl.ResumeLayout(true);
        }
        /// <summary>Zrusenie vsetkych panelov okrem zadaneho</summary>
        /// <param name="pExceptionIndex">vynimka</param>
        public void DeleteAllPanels(int pExceptionIndex)
        {
            CanPanelActivate = false;
            FTabCtrl.Tag = 1;
            FTabCtrl.SuspendLayout();

            IBaseAppPanel CurrP = ((ITabPage)FTabCtrl.TabPages[pExceptionIndex]).AppPanel;

            for (int i = FTabCtrl.TabCount - 1; i > pExceptionIndex; i--)
            {
                ITabPage TP = (ITabPage)FTabCtrl.TabPages[i]; // Ziskam zalozku
                if (!TP.AppPanel.CanClose) continue; // Neda sa zrusit
                TP.AppPanel.Close(); // Zrusim panel
                TP.AppPanel.Dispose(); // Zrusim panel
                FTabCtrl.TabPages.RemoveAt(i); TP.Dispose(); // Zrusim zalozku
            }

            for (int i = pExceptionIndex - 1; i >= 0; i--)
            {
                ITabPage TP = (ITabPage)FTabCtrl.TabPages[i]; // Ziskam zalozku
                if (!TP.AppPanel.CanClose) continue; // Neda sa zrusit
                TP.AppPanel.Close(); // Zrusim panel
                TP.AppPanel.Dispose(); // Zrusim panel
                FTabCtrl.TabPages.RemoveAt(i); TP.Dispose(); // Zrusim zalozku
            }

            FTabCtrl.Tag = null;
            CanPanelActivate = true;

            SetActivePanel(CurrP); // Aktivujem panel
            FTabCtrl.ResumeLayout(true);
        }
        /// <summary>Zrusenie vsetkych panelov vpravo od zadaneho</summary>
        /// <param name="pIndex">vynimka</param>
        public void DeleteAllPanelsOnRight(int pIndex)
        {
            CanPanelActivate = false;
            FTabCtrl.Tag = 1;
            FTabCtrl.SuspendLayout();

            for (int i = FTabCtrl.TabCount - 1; i > pIndex; i--)
            {
                ITabPage TP = (ITabPage)FTabCtrl.TabPages[i]; // Ziskam zalozku
                if (!TP.AppPanel.CanClose) continue; // Neda sa zrusit
                TP.AppPanel.Close(); // Zrusim panel
                TP.AppPanel.Dispose(); // Zrusim panel
                FTabCtrl.TabPages.RemoveAt(i); TP.Dispose(); // Zrusim zalozku
            }

            FTabCtrl.Tag = null;
            CanPanelActivate = true;

            SetActivePanel(pIndex); // Aktivujem panel
            FTabCtrl.ResumeLayout(true);
        }

        /// <summary>Vytvorenie noveho panela a zaradenie medzi zalozky</summary>
        /// <param name="pID">ID</param>
        /// <param name="pInitStr">inicializacny retazec</param>
        /// <returns>objekt panela</returns>
        public IBaseAppPanel AddNewPanel(int pID, string pInitStr = "")
        {
            IBaseAppPanel P = IBaseAppPanel.CreateAppPanel(pID, FTabCtrl.TabCount); // Volam vytvorenie panela
            if (P == null) return null; // Chyba?

            ITabPage Page = new ITabPage();
            Page.SuspendLayout();
            P.SuspendLayout();

            Page.AppPanel = P; P.Dock = System.Windows.Forms.DockStyle.Fill; // Vytvorim zalozku a pripojim panel
            Page.Controls.Add(P);

            P.LoadSettings(); // Nacitam posledne nastavenie
            if (!P.Init(pInitStr))
            {
                P.Dispose(); Page.Dispose();
                return null;
            }

            FTabCtrl.TabPages.Add(Page); // Zaradim zalozku
            P.ResumeLayout(); Page.ResumeLayout(); Page.Text = P.TabName; // Nastavim nazov

            return P; // Vratim panel
        }
        /// <summary>Nastavenie aktivneho panela</summary>
        /// <param name="pPanel">panel</param>
        public void SetActivePanel(IBaseAppPanel pPanel)
        {
            CanPanelActivate = false;
            int I = FindPanelIndex(pPanel); // Hladam poziciu
            if (I == -1) return; // Nenasiel som
            FTabCtrl.Tag = 1; FTabCtrl.SelectedIndex = I; FTabCtrl.Tag = null;

            UpdateMainMenu(); // Aktualizujem menu

            System.Windows.Forms.Application.DoEvents(); // Aktualizujem formular
            pPanel.ActivatePanel(); // Aktivujem panel
            CanPanelActivate = true;
        }
        /// <summary>Nastavenie aktivneho panela</summary>
        /// <param name="pIndex">pozicia</param>
        public void SetActivePanel(int pIndex)
        {
            if ((pIndex < 0) || (pIndex >= FTabCtrl.TabCount)) return; // Mimo rozsah?
            SetActivePanel(((ITabPage)FTabCtrl.TabPages[pIndex]).AppPanel); // Volam aktivaciu
        }
        /// <summary>Nastavenie nazvu zalozky</summary>
        /// <param name="pPanel">panel</param>
        /// <param name="pValue">nazov</param>
        public void SetTabPageName(IBaseAppPanel pPanel, string pValue)
        {
            int I = FindPanelIndex(pPanel); // Najdem poziciu panela
            if (I != -1) FTabCtrl.TabPages[I].Text = pValue; // Nastavim nazov
        }
        /// <summary>Nastavenie ikony na zalozke</summary>
        /// <param name="pPanel">panel</param>
        /// <param name="pIconIndex">cislo ikony alebo -1 pre ziadnu</param>
        public void SetTabIcon(IBaseAppPanel pPanel, int pIconIndex = -1)
        {
            int I = FindPanelIndex(pPanel); // Najdem poziciu panela
            if (I != -1) FTabCtrl.TabPages[I].ImageIndex = pIconIndex; // Nastavim nazov
        }
    }
}
